package cn.tedu.charging.order.service.impl;

import cn.tedu.charging.common.pojo.JsonResult;
import cn.tedu.charging.common.pojo.param.GunStatusUpdateParam;
import cn.tedu.charging.common.pojo.utils.JsonUtils;
import cn.tedu.charging.common.pojo.vo.StationInfoVO;
import cn.tedu.charging.common.pojo.vo.UserInfoVO;
import cn.tedu.charging.order.constant.Constant;
import cn.tedu.charging.order.constant.MqttConstant;
import cn.tedu.charging.order.feign.DeviceClient;
import cn.tedu.charging.order.feign.UserClient;
import cn.tedu.charging.order.job.DeviceCheckJob;
import cn.tedu.charging.order.mqtt.MqttProducer;
import cn.tedu.charging.order.pojo.dto.CarInfoDto;
import cn.tedu.charging.order.pojo.dto.ChargingDto;
import cn.tedu.charging.order.pojo.dto.StationInfoDto;
import cn.tedu.charging.order.pojo.param.OrderAddParam;
import cn.tedu.charging.order.pojo.po.OrderMQPO;
import cn.tedu.charging.order.rabbitmq.RabbitMQOrderProducer;
import cn.tedu.charging.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private UserClient userClient;
    @Autowired
    private DeviceClient deviceClient;


    @Override
    public String createOder(OrderAddParam orderAddParam) throws Exception {
        //1 创建订单号
        String orderNo= getOrderNo();
        log.debug("生成订单号:{}",orderNo);

        //创建定时器任务
        DeviceCheckJob deviceCheckJob=new DeviceCheckJob(orderNo,orderAddParam.getGunId());

        //2 获取运营商id
        StationInfoDto stationInfoDto=getStationInfo(orderAddParam.getGunId());
        log.debug("成功获取运营商id:{}",stationInfoDto);

        //3 获取车辆信息
        CarInfoDto carInfoDto=getCarInfo(orderAddParam.getUserId());
        log.debug("成功获取运营商id:{}",stationInfoDto);

        //4 修改枪状态为已使用
        Boolean success=updateGunStatus(orderAddParam.getGunId(), Constant.GUN_STATUS_BUSY);
        log.debug("修改枪状态为已使用:{}",success);
        if(!success){
            log.debug("修改枪状态失败:{}",Constant.GUN_STATUS_BUSY);
        }

        //5 向充电桩发送指令
        startCharging(orderNo,orderAddParam.getPileId(),orderAddParam.getGunId());

        //6 向rabbitMQ发送消息
        sendOrderMessage(orderNo);
        return orderNo;
    }
    @Autowired
    private RabbitMQOrderProducer rabbitMQOrderProducer;

    //6
    private void sendOrderMessage(String orderNo){
        OrderMQPO orderMQPO=new OrderMQPO();
        orderMQPO.setOrderNo(orderNo);
        rabbitMQOrderProducer.sendOrder(orderMQPO);
    }
    //1
    private String getOrderNo() {
        String start="1000";
        Random random=new Random();
        int rand = random.nextInt(100);
        long now = System.currentTimeMillis();
        String orderNo=start+"_"+rand+"_"+now;
        return orderNo;
    }

    //2
    private StationInfoDto getStationInfo(Integer gunId) {
        StationInfoDto stationInfoDto=new StationInfoDto();
        JsonResult<StationInfoVO> stationInfo = deviceClient.getStationInfo(gunId);
        if (stationInfo != null) {
            StationInfoVO data = stationInfo.getData();
            if (data != null) {
                stationInfoDto.setStationId(data.getStationId());
                return stationInfoDto;
            }
        }
        return null;
    }

    //3
    private CarInfoDto getCarInfo(Integer gunId) {
        CarInfoDto carInfoDto=new CarInfoDto();
        JsonResult<UserInfoVO> userInfo = userClient.getUserInfo(gunId);
        if (userInfo != null) {
            UserInfoVO data = userInfo.getData();
            if (data != null) {
                carInfoDto.setCarId(data.getCarId());
                return carInfoDto;
            }
        }
        return null;
    }

    //4
    private Boolean updateGunStatus(Integer gunId,Integer gunStatusBusy) {
        GunStatusUpdateParam gunStatusUpdateParam=new GunStatusUpdateParam();
        gunStatusUpdateParam.setId(gunId);
        gunStatusUpdateParam.setStatus(gunStatusBusy);
        JsonResult<Boolean> booleanJsonResult = deviceClient.updateGunStatus(gunStatusUpdateParam);
        if (booleanJsonResult != null) {
            return booleanJsonResult.getData();
        }
        return false;
    }

    @Autowired
    private MqttProducer mqttProducer;
    //5
    private void startCharging(String orderNo,Integer pileId,Integer gunId) {
        ChargingDto chargingDto=new ChargingDto();
        chargingDto.setOrderNo(orderNo);
        chargingDto.setPileId(pileId);
        chargingDto.setGunId(gunId);
        chargingDto.setMsg(Constant.START_CHARGING);
        String json = JsonUtils.toJson(chargingDto);
        String topic= MqttConstant.TOPIC_START_CHARGING_PREFIX+chargingDto.getPileId();
        mqttProducer.send(topic,json);
    }
}
